package com.guigu.tms.aggregation.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.guigu.tms.aggregation.mapper.OrderMapper;
import com.guigu.tms.commons.aggregation.entity.Order;
import com.guigu.tms.aggregation.mapper.AuthUserMapper;
import com.guigu.tms.aggregation.mapper.CoreOrgMapper;
import com.guigu.tms.commons.aggregation.entity.AuthUser;
import com.guigu.tms.commons.aggregation.entity.TaskPickupDispatch;
import com.guigu.tms.aggregation.mapper.TaskPickupDispatchMapper;
import com.guigu.tms.aggregation.service.ITaskPickupDispatchService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 取件、派件任务信息表 服务实现类
 * </p>
 *
 * @author wj
 * @since 2022-10-21
 */
@Service
public class TaskPickupDispatchServiceImpl extends ServiceImpl<TaskPickupDispatchMapper, TaskPickupDispatch> implements ITaskPickupDispatchService {

    @Autowired
    TaskPickupDispatchMapper taskPickupDispatchMapper;

    @Autowired
    OrderMapper Task;

    @Autowired
    private AuthUserMapper userMapper;
    @Autowired
    private CoreOrgMapper coreOrgMapper;


    @Override
    public void CreateTaskPickupDispatch(List<String> orderId) {
        for (int i = 0; i < orderId.size(); i++) {
            String s = orderId.get(i);
            QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("order_id", s);
            //Order order = orderMapper.selectOne(queryWrapper);
            TaskPickupDispatch taskPickupDispatch = new TaskPickupDispatch();
            taskPickupDispatch.setOrderId(s);
            taskPickupDispatch.setTaskType(2);
            taskPickupDispatch.setStatus(1);
            //taskPickupDispatch.setAgencyId(order.get);
        }
    }

    @Override
    public TaskPickupDispatch getTaskPickupDispatchByOrderId(String orderId,Integer type) {
        QueryWrapper<TaskPickupDispatch> wrapper = new QueryWrapper<>();
        System.out.println(orderId);
        wrapper.eq("order_id",orderId);
        wrapper.eq("task_type",type);
        TaskPickupDispatch taskPickupDispatch = taskPickupDispatchMapper.selectOne(wrapper);

        if(taskPickupDispatch!=null) {
            if(taskPickupDispatch.getAgencyId()!=null) {
                taskPickupDispatch.setCourier(userMapper.selectById(taskPickupDispatch.getCourierId()));
            }
            taskPickupDispatch.setCoreOrg(coreOrgMapper.selectById(taskPickupDispatch.getAgencyId()));
        }
        System.out.println(taskPickupDispatch);
        return taskPickupDispatch;
    }

}
